package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.PTEDao;
import com.ef.domain.PTE;
import com.ef.domain.Team;
import com.ef.service.PTEService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class PTEServiceImpl extends BaseServiceImpl implements PTEService {

	private PTEDao pTEDao;

	public void delete(PTE entity) {
		entity.setStatus(StatusType.INVALID);
		pTEDao.update(entity);

	}

	public List<PTE> findALLVaild() {
		DetachedCriteria criterion = DetachedCriteria.forClass(PTE.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return pTEDao.findAllByCriteria(criterion);
	}

	public List<PTE> findALLVaildByTeam(Team team) {
		DetachedCriteria criterion = DetachedCriteria.forClass(PTE.class);
		criterion.add(Restrictions.eq("team", team));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return pTEDao.findAllByCriteria(criterion);
	}

	public PaginationSupport<PTE> findByExample(CustomExample<PTE> example,
			int startIndex, int pageSize) {

		return pTEDao.findPageByExample(example, startIndex, pageSize);
	}

	public List<PTE> findByAnything(String qitem, int max) {
		StringBuffer hql = new StringBuffer(
				"From PTE where (lower(name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' OR lower(worknumber) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and status='VALID' ");
		return pTEDao.findMultiAll(hql.toString(), 0, max);

		// DetachedCriteria criterion = DetachedCriteria.forClass(PTE.class);
		// Disjunction or = Restrictions.disjunction();
		// Property myProperty = Property.forName("name");
		// or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
		// or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
		// criterion.add(or);
		// criterion.add(Restrictions.eq("status",
		// StatusType.VALID));
		// return pTEDao.findAllByCriteria(criterion, max);
	}

	public List<PTE> findByAnythingWithTeam(String qitem, int max,Team team) {
		StringBuffer hql = new StringBuffer(
				"From PTE where (lower(name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' OR lower(worknumber) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and status='VALID' and team_id=").append(team.getId());
		return pTEDao.findMultiAll(hql.toString(), 0, max);

		// DetachedCriteria criterion = DetachedCriteria.forClass(PTE.class);
		// Disjunction or = Restrictions.disjunction();
		// Property myProperty = Property.forName("name");
		// or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
		// or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
		// criterion.add(or);
		// criterion.add(Restrictions.eq("status",
		// StatusType.VALID));
		// return pTEDao.findAllByCriteria(criterion, max);
	}
	
	public PTE findByWorkNo(String workNo) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PTE.class);
		criteria.add(Restrictions.eq("worknumber", workNo.trim()));
		criteria.add(Restrictions.eq("status", StatusType.VALID));
		List<PTE> ptes = pTEDao.findAllByCriteria(criteria);
		if (ptes != null && ptes.size() > 0) {
			return ptes.get(0);
		}
		return null;
	}

	public PTE findByDuplicatePte(PTE pte) {
		StringBuffer hql = new StringBuffer(
				"From PTE where (lower(worknumber) = '");
		hql.append(pte.getWorknumber().trim().toLowerCase());
		hql.append("') and status='VALID' and id<>");
		hql.append(pte.getId());
		List<PTE> ptes = pTEDao.findMultiAll(hql.toString(), 0, 10);
		//	    
		// DetachedCriteria criteria = DetachedCriteria.forClass(PTE.class);
		// criteria.add(Restrictions.eq("worknumber",
		// pte.getWorknumber().trim()));
		// List<PTE> ptes = pTEDao.findAllByCriteria(criteria);
		if (ptes != null && ptes.size() > 0) {
			return ptes.get(0);
		}
		return null;
	}

	public PTE findById(Long id) {

		return pTEDao.findById(id);
	}

	public void saveOrUpdate(PTE entity) {

		pTEDao.saveOrUpdate(entity);
	}

	public PTEDao getpTEDao() {
		return pTEDao;
	}

	public void setpTEDao(PTEDao pTEDao) {
		this.pTEDao = pTEDao;
	}

}
